/**
 * Created by lenovo on 2018/3/10.
 */
var map;
var popup;
var vector_layer;
var feature=null;
var wkt_format = new OpenLayers.Format.WKT();
$(function () {
    loadMap(map);
})
function loadMap(map)
{
    var baselayer;
    var mapOptions = {
        resolutions: [8.466683600033868, 4.233341800016934, 2.116670900008467, 1.0583354500042335, 0.5291677250021167, 0.26458386250105836, 0.13229193125052918, 0.06614596562526459, 0.033072982812632296],
        projection: new OpenLayers.Projection('EPSG:3857'),
        maxExtent: new OpenLayers.Bounds(-20037700.0,3416478.884091098,13568937.879942432,30241100.0),
        units: "meters",
        controls: [new OpenLayers.Control.Navigation()]
    };
    map = new OpenLayers.Map('allmap', mapOptions );
    vector_layer= new OpenLayers.Layer.Vector();
    baselayer = new OpenLayers.Layer.WMS(
        "vec","http://localhost:8080/geowebcache/service/wms",
        {layers: 'vec', format: 'image/png' },
        { tileSize: new OpenLayers.Size(256,256),
            tileOrigin: new OpenLayers.LonLat(-2.00377E7, 3.02411E7)});
    map.addLayer(baselayer);
    map.addLayer(vector_layer);
    map.zoomToExtent(new OpenLayers.Bounds(13562372.424596021,3417220.785561478,13567900.096120471,3420305.8333982406));
    map.events.register('click', map, function (e) {
        var pixel = new OpenLayers.Pixel(e.xy.x,e.xy.y);
        var lonlat = map.getLonLatFromPixel(pixel);
        // lonlat.transform(new OpenLayers.Projection("EPSG:3857"), new OpenLayers.Projection("EPSG:4326"));
        $.post("/fw/getFWInfo",{
            x:lonlat.lon,
            y:lonlat.lat
        },function (data) {
            if(data=="")
            {
                return;
            }else
            {
                showPopup(map,lonlat,data)
            }
        })

    })
}
function glGeom(data) {
    //首先删除以前的高亮
    //高亮数据
    
}
function showPopup(map,lonlat,data){
    var dc,dn;
    if(data.doorid==null) {
        dc = "------"
    }else{
        dc = data.doorid
    }
    if(data.doorname==null)
    {
        dn="------"
    }else
    {
        dn=data.doorname;
    }
    if(popup!=null){
        map.removePopup(popup);
        vector_layer.removeFeatures([feature])
        popup=new OpenLayers.Popup("popup",
            lonlat,
            new OpenLayers.Size(120,100),
            "<div od='popinfo' style='background-color: white;'>" +
            "<br/>" +
            "<div style='padding-left: 15px'>" +
            "<div>门牌号：<span id='doorid'>"+dc+"</span></div>" +
            "<div>户主：<span id='doorname'>"+dn+"</span></div>" +
            "<div style='padding-left: 35px'><a style='cursor: pointer' onclick='updateInfo("+data.gid+")'>修改信息</a></div>" +
            "</div>" +
            "</div>",
            true,closePopup);
        map.addPopup(popup);
        feature=wkt_format.read(data.geom);
        vector_layer.addFeatures([feature]);
    }else
    {
        popup=new OpenLayers.Popup("popup",
            lonlat,
            new OpenLayers.Size(120,100),
            "<div od='popinfo' style='background-color: white;'>" +
            "<br/>" +
            "<div style='padding-left: 15px'>" +
            "<div>门牌号：<span id='doorid'>"+dc+"</span></div>" +
            "<div>户主：<span id='doorname'>"+dn+"</span></div>" +
            "<div style='padding-left: 35px'><a style='cursor: pointer' onclick='updateInfo("+data.gid+")'>修改信息</a></div>" +
            "</div>" +
            "</div>",
            true,closePopup);
        map.addPopup(popup);
        feature=wkt_format.read(data.geom);
        vector_layer.addFeatures([feature]);

    }
    function closePopup(){
        map.removePopup(popup);
        vector_layer.removeFeatures([feature])
        popup=null
        feature=null;
        //同时高亮去除
    }
}
function createGeoJson(input){
}

